﻿namespace WFBot.Workflow
{
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data.Common;
    using System.Linq;
    using Microsoft.Practices.Unity;
    using WFBot.Data;
    using WFBot.Framework.Infrastructure;
    using WFBot.Framework.Repositories;
    using WFBot.Framework.Services;
    using WFBot.Workflow;
    using WFBot.Data.Repository;
    using WFBot.Framework.Domain;

    public class IoC
    {
        private static readonly Func<LifetimeManager> singleton = () => new ContainerControlledLifetimeManager();

        public static void Register(IUnityContainer container)
        {
            container.RegisterType<IUnitOfWork, UnitOfWork>(singleton());

            container.RegisterType<IQuestionRepository, QuestionRepository>(singleton());
            container.RegisterType<IResponseRepository, ResponseRepository>(singleton());
            container.RegisterType<IVisitorRepository, VisitorRepository>(singleton());
            container.RegisterType<IOperatorRepository, OperatorRepository>(singleton());
            container.RegisterType<IRepository<Question>, QuestionRepository>(singleton());
            container.RegisterType<IRepository<Response>, ResponseRepository>(singleton());
            container.RegisterType<IRepository<Visitor>, VisitorRepository>(singleton());
            container.RegisterType<IRepository<Operator>, OperatorRepository>(singleton());
            container.RegisterType<IBotService, BotService>(singleton());
            
            ConnectionStringSettings connectionStringSettings = ConfigurationManager.ConnectionStrings["WFBotDatabase"];
            string providerName = connectionStringSettings.ProviderName;

            DbProviderFactory databaseProviderFactory = DbProviderFactories.GetFactory(providerName);
            container.RegisterInstance(databaseProviderFactory);

            container.RegisterType<IDatabaseFactory, DatabaseFactory>(singleton(), new InjectionConstructor(typeof(DbProviderFactory), connectionStringSettings.ConnectionString));
        }
    }
}
